package com.example.genericrpg_android;

import ControllerLayer.BattleController;
import ControllerLayer.Controller;
import ControllerLayer.PauseMenuController;
import ControllerLayer.RoomController;
import ControllerLayer.TitleMenuController;
import ModelLayer.Game;
import ModelLayer.Menu;
import ServiceLayer.Input;
import android.content.Context;
import android.graphics.Canvas;

public class GameEngine {
	private Game gameModel;
	
	public Controller currentController;
	public TitleMenuController titleMenuController; 
	public RoomController roomController;
	public BattleController battleController;
	public PauseMenuController pauseMenuController;
	
	public Context activity;
	public GameView gameView;
	public Input input;
	
	private boolean firstRun;
	public int x=0,y=0;
	
	public GameEngine(Context activity, GameView gameView) {
		this.activity = activity;
		this.gameView = gameView;
		
		firstRun = true;
		this.input = new Input(activity);
		input.initializeVariables(gameView);
		
		gameModel = new Game(activity);
		
		titleMenuController = new TitleMenuController(gameModel.getTitleMenu(), gameModel, this, activity);
		roomController = new RoomController(gameModel, this, activity);
		battleController = new BattleController(gameModel, this, activity);
		pauseMenuController = new PauseMenuController(gameModel, this, activity);
		
		currentController = titleMenuController;
		currentController.playMusic();
		printControllerName();
	}
	
	
	public void handleInput(float X, float Y) {
		this.x = (int)X;
		this.y = (int)Y;
		if (x != -1 && y != -1) {
			if (input.up.contains(x,y)) {
				currentController.handleInput(Input.UP);
				if (currentController == roomController)
					gameView.move = true;
			} else if (input.right.contains(x,y)) {
				currentController.handleInput(Input.RIGHT);
				if (currentController == roomController)
					gameView.move = true;
			} else if (input.down.contains(x,y)) {
				currentController.handleInput(Input.DOWN);
				if (currentController == roomController)
					gameView.move = true;
			} else if (input.left.contains(x,y)) {
				currentController.handleInput(Input.LEFT);
				if (currentController == roomController)
					gameView.move = true;
			} else if (input.a.contains(x,y)) {
				currentController.handleInput(Input.A);
			} else if (input.b.contains(x,y)) {
				currentController.handleInput(Input.B);
			}
		}
	}
	
	public void handleDown(float X, float Y){
		this.x = (int)X;
		this.y = (int)Y;
		if (x != -1 && y != -1) {
			if (input.up.contains(x,y)) {
				currentController.handleInput(Input.UP);
			} else if (input.right.contains(x,y)) {
				currentController.handleInput(Input.RIGHT);
			} else if (input.down.contains(x,y)) {
				currentController.handleInput(Input.DOWN);
			} else if (input.left.contains(x,y)) {
				currentController.handleInput(Input.LEFT);
			}
		}
	}
	
	public void handleUP(float X, float Y) {
		this.x = (int)X;
		this.y = (int)Y;
		if (currentController == roomController)
			if (x != -1 && y != -1) {
				if (input.up.contains(x,y)) {
					gameView.move = false;
					currentController.handleInput(-1);
				} else if (input.right.contains(x,y)) {
					gameView.move = false;
					currentController.handleInput(-1);
				} else if (input.down.contains(x,y)) {
					gameView.move = false;
					currentController.handleInput(-1);
				} else if (input.left.contains(x,y)) {
					gameView.move = false;
					currentController.handleInput(-1);
				} 
			}
	}
	
	/** the update method with the deltaTime in seconds **/
	public void update() {
		currentController.update();
	}
	
	public void render(Canvas g) {
		if (firstRun) {
			input.initializeVariables(gameView);
			firstRun = !firstRun;
		}
		currentController.getView().render(g, gameView);
		g.drawBitmap(input.dPad, input.dPadSource, input.dPadDest, null);
		g.drawBitmap(input.aButton, input.aSource, input.aDest, null);
		g.drawBitmap(input.bButton, input.bSource, input.bDest, null);

		//		g.drawBitmap(input.pauseButton, input.pauseSource, input.pauseDest, null);
	}
	
	public void printControllerName(){
		System.out.println("CONTROLLER>>"+currentController.getClass().getSimpleName()+"");
		printViewName();
	}
	
	public void printViewName() {
		System.out.println("View::"+currentController.getView().getClass().getSimpleName());
	}
	
	public void startNewBattle() {
		battleController.startNewBattle();
		currentController = battleController;
		currentController.playMusic();
	}
	
	public void setController(int controller){
		switch(controller){
		case Controller.RoomController:
			currentController = roomController;
			break;
		case Controller.BattleController:
			currentController = battleController;
			break;
		case Controller.PauseMenuController:
			currentController = pauseMenuController;
			break;
		case Controller.TitleMenuController:
			currentController = titleMenuController;
			break;
		}
	}
	
	public void loadGame() {
		Menu m = gameModel.getTitleMenu();
		titleMenuController = new TitleMenuController(m, gameModel, this, activity);
		roomController = new RoomController(gameModel, this, activity);
		currentController = roomController;
		m.reset();
		currentController.playMusic();
	}
	
	public void startNewGame() {
		gameModel = new Game(activity);
		gameModel.newGameStats();
		
		titleMenuController = new TitleMenuController(gameModel.getTitleMenu(), gameModel, this, activity);
		roomController = new RoomController(gameModel, this, activity);
		battleController = new BattleController(gameModel, this, activity);
		pauseMenuController = new PauseMenuController(gameModel, this, activity);
		
		currentController = roomController;
		currentController.playMusic();
		printControllerName();
	}
	
	public void pause() {
		currentController.stopMusic();
	}
	
	public void resume() {
		currentController.playMusic();
	}
}
